package cn.yuan.xiaoyu.newsmodule.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import butterknife.Bind;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.fragment.BaseFragment;
import cn.yuan.xiaoyu.commonmodule.view.recycleview.YRecycleview;
import cn.yuan.xiaoyu.newsmodule.adapter.NewsListDataAdapter;
import cn.yuan.xiaoyu.newsmodule.bean.TouTiaolistBean;
import cn.yuan.xiaoyu.newsmodule.presenter.INewsHomeListVIew;
import cn.yuan.xiaoyu.newsmodule.presenter.NewsHomeListPresenter;

/**
 * Created by yukuoyuan on 2017/3/7.
 * 这是一个新闻列表的界面
 */

public class NewsHomeListFragment extends BaseFragment implements INewsHomeListVIew, YRecycleview.RefreshAndLoadMoreListener {

    @Bind(R.id.rcv_news_list)
    YRecycleview rcvNewsList;
    private NewsHomeListPresenter newsHomeListPresenter;
    private String query;
    private boolean isRefresh = true;
    NewsListDataAdapter newsListDataAdapter = null;

    public static NewsHomeListFragment newInstance(String query) {
        NewsHomeListFragment fragment = new NewsHomeListFragment();
        Bundle args = new Bundle();
        args.putString("QUERY", query);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initdata(Bundle arguments) {
        Log.d("参数", arguments.getString("QUERY"));
        query = arguments.getString("QUERY");
        layoutLoading();
        newsHomeListPresenter.getListData(query);
        rcvNewsList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rcvNewsList.setRefreshAndLoadMoreListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_newshomelist;
    }

    @Override
    protected void bindPresenter() {
        newsHomeListPresenter = new NewsHomeListPresenter(this, getActivity());
    }

    @Override
    protected void initToolbar() {

    }

    @Override
    public void showT(String msg) {
        showToast(getActivity(), msg);
    }

    @Override
    public void showL(String msg) {

    }

    @Override
    public void closeL() {

    }

    @Override
    public void setAdapterData(TouTiaolistBean touTiaolistBean) {
//        layoutContent();
//        rcvNewsList.setReFreshComplete();
//        rcvNewsList.loadMoreComplete();
//        if (isRefresh) {
//            List<TouTiaolistBean.DataBean> list;
//            list = touTiaolistBean.data;
//            newsListDataAdapter = new NewsListDataAdapter(list, getActivity());
//            rcvNewsList.setAdapter(newsListDataAdapter);
//        } else {
//            newsListDataAdapter.addData(touTiaolistBean.data);
//        }
//        newsListDataAdapter.setOnItemClickListener(new NewsListDataAdapter.OnItemClickListener() {
//            @Override
//            public void onNewsItemClick(String source_url) {
//                //新闻类型的
//                Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);
//                intent.putExtra("URL", "http://m.toutiao.com".concat(source_url));
//                startActivity(intent);
//            }
//
//            @Override
//            public void onVideoItemClick(String source_url) {
//                //视频类型的e
//            }
//        });
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        newsHomeListPresenter.getListData(query);
    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        newsHomeListPresenter.getListData(query);

    }
}
